package com.hejing.List不同实现类的对比;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * ClassName: ListTest
 * Package: com.hejing.List不同实现类的对比
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-05-18 9:50
 * @Version: 1.0
 */
public class ListTest {
    public static void main(String[] args) {

        // 1、随机生成30个字符，存放在ArrayList中
        List list = new ArrayList();
        for(int i = 0; i < 30; i++) {
            int charNum = (int)(Math.random() * (122 - 97 + 1)) + 97;
            list.add((char)charNum + "");
        }
        // 2、遍历ArrayList，查找指定元素出现的次数
        System.out.println(list);

        int count = listTest(list, "a");
        System.out.println("count: " + count);

    }

    public static int listTest(Collection list, String s) {
        int count = 0;
        for(Object obj: list) {
            if (s.equals(obj)) {
                count++;
            }
        }
        return count;
    }
}
























